package cn.xd.itstack.springframework.core.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Classname UrlResource
 * @Description TODO
 * @Date 2022/5/16 上午11:36
 * @Created by lvxiaodong
 */
public class UrlResource implements Resource {


    private  URL url;


    public UrlResource(URL url){
        this.url = url;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        URLConnection urlConnection = null;
        try {
            urlConnection = this.url.openConnection();
            return urlConnection.getInputStream();
        }catch (Exception e){
            if (urlConnection instanceof HttpURLConnection){
                ((HttpURLConnection) urlConnection).disconnect();
            }
            throw e;
        }


    }
}
